﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXQ.Dao.ViewModel;
using AutoMapper;
namespace YXQ.Dao
{

    public class GCBDao
    {


        public GCBDao()
        {

            Mapper.CreateMap<Y_GCB, GCBModel>();
        }
        public List<Y_GCB> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                recordcount = edm.Y_GCB.Count();

                return edm.Y_GCB.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public GCBModel GetOneById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_GCB.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<Y_GCB, GCBModel>(query);
            }

        }


        public int Save(GCBModel obj)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_GCB.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new Y_GCB();

                    edm.AddToY_GCB(query);

                }


                query.ProjectId = obj.ProjectId;

                query.gctj = obj.gctj;

                query.tjwt = obj.tjwt;

                query.ldjj = obj.ldjj;

                query.xyxt = obj.xyxt;

                query.gczl = obj.gczl;

                query.gcaq = obj.gcaq;

                query.gcysrq = obj.gcysrq;
                query.sjysrq = obj.sjysrq;
                query.jlysrq = obj.jlysrq;

                query.createuserid = obj.createuserid;

                query.createusername = obj.createusername;
                query.createrealname = obj.createrealname;
                query.createtime = obj.createtime;

                edm.SaveChanges();

                return query.Id;


            }

        }

        public GCBModel GetOneByProjectId(int id, int reid)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_GCB.Where(s => s.ProjectId == id && s.Id == reid);



                return Mapper.Map<Y_GCB, GCBModel>(query.OrderByDescending(s => s.Id).FirstOrDefault());
            }
        }



        /// <summary>
        /// 获取最后一次修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public GCBModel GetOneByProjectId(int id)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_GCB.Where(s => s.ProjectId == id);

                return Mapper.Map<Y_GCB, GCBModel>(query.OrderByDescending(s => s.Id).FirstOrDefault());
            }
        }


        public List<GCBModel> GetByProjectId(int id)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_GCB.Where(s => s.ProjectId == id);



                return Mapper.Map<List<Y_GCB>, List<GCBModel>>(query.OrderByDescending(s => s.Id).ToList());
            }
        }

        public int DelById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_GCB.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

